package jgl.vajra.operator.part.mine.activity;

import android.text.TextUtils;

import com.bigkoo.pickerview.view.OptionsPickerView;

import java.util.Map;

import io.reactivex.Observable;
import jgl.vajra.operator.R;
import jgl.vajra.operator.bean.WalletDetailedBean;
import jgl.vajra.operator.databinding.ActivityWalletDetailedBinding;
import jgl.vajra.operator.manager.AccountManager;
import jgl.vajra.operator.part.mine.contract.WalletDetailedContract;
import jgl.vajra.operator.part.mine.pop.AllPeriodsPop;
import jgl.vajra.operator.part.mine.pop.ConditionTypePop;
import jgl.vajra.operator.part.mine.viewModel.WalletDetailedViewModel;
import jgl.vajra.operator.widget.bean.BaseListData;
import jgl.vajra.operator.widget.databindingadapter.SingleTypeBindingAdapter;
import jgl.vajra.operator.widget.interfaces.RefreshRecyclerLoadStatusListener;
import jgl.vajra.operator.widget.mvvm.factory.CreateViewModel;
import jgl.vajra.operator.widget.mvvm.view.BaseMVVMActivity;
import jgl.vajra.operator.widget.recyclerView.RefreshRecyclerNetConfig;
import jgl.vajra.operator.widget.utils.UIUtils;

@CreateViewModel(WalletDetailedViewModel.class)
public class WalletDetailedActivity extends BaseMVVMActivity<WalletDetailedViewModel, ActivityWalletDetailedBinding> implements WalletDetailedContract.View {

    private AllPeriodsPop allPeriodsPop;
    private ConditionTypePop conditionTypePop;
    private String time = "";
    private String conditionType = "";

    @Override
    public int getLayoutId() {
        return R.layout.activity_wallet_detailed;
    }

    @Override
    public void initView() {
        super.initView();
        mBinding.setView(this);

        mBinding.recyclerView.setIsDataObject(true);
        mBinding.recyclerView.setIsShowPageManager(true);
        SingleTypeBindingAdapter adapter = new SingleTypeBindingAdapter(this, null, R.layout.item_wallet_detailed);
        mBinding.recyclerView.setAdapter(adapter);
        mBinding.recyclerView.setRefreshRecyclerNetConfig(new RefreshRecyclerNetConfig() {
            @Override
            public Observable getNetObservable(Map<String, Object> map, int action) {
                map.put("Authorization", AccountManager.getInstance().getLoginData(UIUtils.getContext()).authorization);

                map.put("whereEnd", time + conditionType);

                return mViewModel.getWalletDetailedListData(map);
            }
        });

        mBinding.recyclerView.setRefreshLoaderMore(false, false);
        mBinding.recyclerView.setRefreshRecyclerLoadStatusListener(new RefreshRecyclerLoadStatusListener<BaseListData<WalletDetailedBean>>() {
            @Override
            public void onSucceed(BaseListData<WalletDetailedBean> data, int action) {
                mBinding.recyclerView.setRefreshLoaderMore(true, true);
                mBinding.recyclerView.requestNetObjectSuccess(data.data, action);
            }

            @Override
            public void onError(String errorInfo, int errorCode, int action) {
                super.onError(errorInfo, errorCode, action);
                //mBinding.recyclerView.requestNetError(errorInfo, errorCode);
            }
        });
        mBinding.recyclerView.firstLoad();
    }

    @Override
    public void onAllPeriods() {
        if (allPeriodsPop == null)
            allPeriodsPop = new AllPeriodsPop(this);
        allPeriodsPop.setAllPeriodsListener((year, month) -> {
            if (TextUtils.isEmpty(year)) {
                time = "";
            } else {
                time = "and (YEAR(createDate) = " + year + " and MONTH(createDate) =" + month + ")";
            }
            mBinding.recyclerView.firstLoad();
        });
        allPeriodsPop.showPopupWindow(mBinding.llcView);
    }

    @Override
    public void onConditionType() {
        if (conditionTypePop == null)
            conditionTypePop = new ConditionTypePop(this);
        conditionTypePop.setConditionTypeListener((conditionType) -> {
            this.conditionType = conditionType;
            mBinding.recyclerView.firstLoad();
        });
        conditionTypePop.showPopupWindow(mBinding.llcView);
    }
}
